package com.version1_3.controller.ThirdPart;

import com.version1_3.service.ThirdPart.WeiXinService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by kuibu on 15/12/28.
 */
@Controller
public class WeiXinController {
    private static final Logger LOGGER = Logger.getLogger(WeiXinController.class);

    @Autowired
    private WeiXinService weiXinService;


    /**
     * 调用接口: http://localhost:8080/kuibuversion1/thirdPart/weixinShare/getSignature
     *
     * @Description: 通过前端传回的动态URL返回对应的微信signnature，用于转发
     */
    @RequestMapping(value = "/thirdPart/weixinShare/getSignature", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getSignature(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        String url = URLDecoder.decode(request.getParameter("url"));
        if (url == null) {
            LOGGER.error("在文章中对三级分类信息进行评论和提供信息源出错,无法获得客户端发送参数: url");
            return "{\"code\":500}";
        } else {
            return weiXinService.getSignature(url);
        }
    }

}
